package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.mapper.UserMapper;
import cn.tedu.springmvc.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HttpController {
  @Autowired
  private UserMapper userMapper;

  @RequestMapping(value = "/getUser",method = RequestMethod.GET)
  public String getUser(){
      List<User> userAll = userMapper.getUserAll();
      return userAll.toString();
  }

    //根据id查询用户信息
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    public User getUserById(int id){
        User user = userMapper.getUser(id);
        return user;
    }
    //添加用户信息
    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    public String saveUser(@RequestBody User user){
        userMapper.insertUser(user);
        return "添加成功";
    }

    //修改用户信息
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public String updateUser(@RequestBody User user){
        userMapper.updateUser(user);
        return "修改成功";
    }

    //根据id删除用户
    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    public String deleteUser(int id){
        userMapper.deleteUser(id);
        return "删除成功";
    }
}
